var rldMap = null;//风险级别字典
/**
 * 通知管理初始化
 */
var UploadList = {
    id: "patientListTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
UploadList.initColumn = function () {
    return [
        {
            width: 30, align: "center", valign: 'middle', sortable: true, formatter:
                function (value, row, index) {
                    //获取每页显示的数量
                    var pageSize = $('#patientListTable').bootstrapTable('getOptions').pageSize;
                    //获取当前是第几页
                    var pageNumber = $('#patientListTable').bootstrapTable('getOptions').pageNumber;
                    //返回序号，注意index是从0开始的，所以要加上1
                    return pageSize * (pageNumber - 1) + index + 1;
                }
        },
        {title: '病种名称', field: 'entityName', align: 'center', width: 180, valign: 'middle', sortable: false},
        {title: '姓名', field: 'caseName', align: 'center', width: 70, valign: 'middle', sortable: false},
        {title: '病案号', field: 'caseMrno', align: 'center', width: 80, valign: 'middle', sortable: false},
        {title: '出院科室', field: 'dishDept', align: 'center', width: 120, valign: 'middle', sortable: false},
        {title: '入院日期', field: 'admDate', align: 'center', width: 180, valign: 'middle', sortable: false},
        {title: '出院日期', field: 'dishDate', align: 'center', width: 180, valign: 'middle', sortable: false},
        {title: '上报日期', field: 'reportDate', align: 'center', width: 180, valign: 'middle', sortable: false},
        {title: '状态', field: 'reportState', align: 'center', width: 120, valign: 'middle', sortable: false,
            formatter: function (value, row, index) {
                if (2 == value) {
                    return "<span style='color:#50c878;'>上传成功</span>";
                } else if (4 == value) {
                    return "<span style='color:#ff461f;'>上传失败</span>";
                }
            }
        },
        {title: '备注', field: 'reportMsg', align: 'center', width: 240, valign: 'middle', sortable: false},
        {
            title: '操作', align: 'center', width: 120, valign: 'middle', sortable: false, formatter:
                function (value, row, index) {
                    var _html = '';
                    if (row.reportState != 2) {
                        _html += '<a href="javascript:UploadList.edit(\'' + row.inCaseId + '\',\'' + row.caseName + '\',\'' + row.caseMrno + '\',\'' + row.dishDate + '\',\'已上报\')" title="填报"><i class="hisui-icon icon-write-order"></i></a>';
                    } else {

                    }
                    _html += '<a href="javascript:UploadList.record(\'' + row.inCaseId + '\',\'' + row.caseMrno + '\',\'' + row.caseName + '\')" title="日志"><i class="hisui-icon icon-clock-record"></i></a>';
                    return _html;
                }
        }
    ];
};

/**
 * 导出数据-不同条件
 */
UploadList.exportExcel = function () {

    var startDate = $('#startDate').datebox('getValue');
    var endDate = $('#endDate').datebox('getValue');
    var queryData = {};
    queryData['uploadState'] = $('#uploadState').datebox('getValue');    //统计类型
    queryData['startDate'] = startDate;    //获取页面开始时间
    queryData['endDate'] = endDate;    //获取页面结束时间
    queryData['office'] = $('#office').datebox('getValue');//部门
    queryData['depart'] = $('#depart').datebox('getValue');//科室
    queryData['entityType'] = $('#entityType').datebox('getValue');//病种类别
    queryData['name'] = $('#name').val();//患者姓名
    queryData['mrNo'] = $('#mrNo').val();//住院号

    if (queryData['startDate'] != "") {
        queryData['startDate'] = queryData['startDate'] + " 00:00:00";
    }
    if (queryData['endDate'] != "") {
        queryData['endDate'] = queryData['endDate'] + " 23:59:59";
    }
    var longStart = Date.parse(startDate);
    var longEnd = Date.parse(endDate);
    if (longStart > longEnd) {
        Sys.error("开始日期不可大于结束日期");
        return false;
    }

    Sys.loading("数据文件生成中,请耐心等待");
    var ajax = new $ax(Sys.backendPath + "/sds/drgsauto/createExcel", function (data) {
        Sys.closeLoading();
        if (data.message == "DHCC") {
            Sys.info("无数据可以导出，请更改查询条件。");
        } else {
            window.location.href = Sys.backendPath + "/sds/drgsauto/excelDown/" + data.message;
            UploadList.table.refresh();
        }
    }, function (data) {
        Sys.error("数据生成失败!" + data.responseJSON.message + "!");
    });
    ajax.set(queryData);
    ajax.setAsync(true);
    ajax.start();
}

/**
 * 查询病例
 */
UploadList.search = function () {
    var startDate = $('#startDate').datebox('getValue');
    var endDate = $('#endDate').datebox('getValue');
    var queryData = {};
    queryData['uploadState'] = $('#uploadState').datebox('getValue');    //统计类型
    queryData['startDate'] = startDate;    //获取页面开始时间
    queryData['endDate'] = endDate;    //获取页面结束时间
    queryData['office'] = $('#office').datebox('getValue');//部门
    queryData['depart'] = $('#depart').datebox('getValue');//科室
    queryData['entityType'] = $('#entityType').datebox('getValue');//患者类别
    queryData['name'] = $('#name').val();//患者姓名
    queryData['mrNo'] = $('#mrNo').val();//住院号

    if (queryData['startDate'] != "") {
        queryData['startDate'] = queryData['startDate'] + " 00:00:00";
    }
    if (queryData['endDate'] != "") {
        queryData['endDate'] = queryData['endDate'] + " 23:59:59";
    }

    var longStart = Date.parse(startDate);
    var longEnd = Date.parse(endDate);
    if (longStart > longEnd) {
        Sys.error("开始日期不可大于结束日期");
        return false;
    }
    UploadList.table.refresh({query: queryData, pageNumber: 1});
}

/**
 * //根据父级机构 查询子机构
 */
UploadList.officeChange = function (that) {
    var ajax = new $ax(Sys.backendPath + "/sds/common/deptListByHospId", function (data) {
        $('#depart').combobox({
            data: data,
            valueField: 'value',
            textField: 'text',
            defaultFilter: 6,
            onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
                $(this).combobox("setValue", "");
            }
        });
    }, function (data) {
        layer.msg("查询失败!" + data.responseJSON.message + "!", {icon: 2});
    });
    ajax.set({"hospID": that.value, "id": "depart"});
    ajax.start();
}

/**
 * 打开查看病种入组病例详情
 */
UploadList.edit = function (id,name,mrNum,dishDate,state) {
    var flag = false;
    if (id) {
        flag = true;
    } else if (SdsQcincase2.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var title = "姓名:"+name+"|"+"病案号:"+mrNum+"|"+"出院日期:"+dishDate+"|"+"填报状态:"+state;
        var index = Sys.open_c(title, '/sds/sdsqcformshow?mrListId=' + id + '&checkPerm=1&reUploadFlag=1', null, function () {
            UploadList.table.refresh();
        });
        this.layerIndex = index;
    }
};

/**
 * 打开查看病例操作记录
 */
UploadList.record = function (id, mrno, name) {
    var flag = false;
    if (id) {
        flag = true;
    }
    if (flag) {
        var index = Sys.open('【填报日志】姓名：' + name + ' 病案号：' + mrno, Sys.backendPath + '/sds/sdsqcincaseoperlog/' + id, ['800px', '420px']);
        this.layerIndex = index;
    }
};

UploadList.qcFormShow = function (r, e, f) {
    var title = "姓名:"+r.name+"|"+"病案号:"+r.caseMrno+"|"+"出院日期:"+r.dishDate+"|"+"填报状态:已上报";
    var index = Sys.open_c(title, '/sds/sdsqcformshow?mrListId=' + r.incCaseId + '&checkPerm=1&reUploadFlag=1' , null, function (){
        UploadList.table.refresh();
    });
    this.layerIndex = index;
}

$(function () {
    //科室
    $('#depart').combobox({
        data: $.parseJSON($("#departMap").val()),
        valueField: 'value',
        textField: 'text'
    });
    //院区
    $('#office').combobox({
        data: $.parseJSON($("#officeMap").val()),
        valueField: 'value',
        textField: 'text',
        defaultFilter: 6,
        onSelect: UploadList.officeChange,
        onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
            var hospId = $('#hospitalId').val();
            if (hospId != "") {
                $(this).combobox("setValue", hospId);
                $(this).combobox('disable');
            } else {
                var val = $(this).combobox("getData");
                if (val.length == 2) {//如果就一个院区 加上全部=2
                    $(this).combobox("setValue", val[1].value);
                    UploadList.officeChange(val[1]);
                } else {
                    $(this).combobox("setValue", "");
                }
            }
        }
    });
    //病种
    $('#entityType').combobox({
        data: $.parseJSON($("#entityMap").val()),
        valueField: 'value',
        textField: 'text',
        defaultFilter: 6
    });
    // 状态
    $('#uploadState').combobox({
        data: [
            {value: "", text: "全部"},
            {value: "2", text: "上传成功"},
            {value: "4", text: "上传失败"}
        ],
        valueField: 'value',
        textField: 'text',
        defaultFilter: 6
    });
    //渲染datebox --没有渲染的话 图标左右间距不等
    $('.hisui-datebox').datebox({});

    //默认一星期
    $('#startDate').datebox('setValue', timeStamp2String(new Date().getTime() - (1000 * 60 * 60 * 24 * 7)));
    $('#endDate').datebox('setValue', timeStamp2String(new Date().getTime()));

    var startDate = $('#startDate').datebox('getValue');//开始日期
    var endDate = $('#endDate').datebox('getValue');//结束日期
    startDate = startDate + " 00:00:00";
    endDate = endDate + " 23:59:59";
    var uploadState = $('#uploadState').combobox('getValue');
    var entityType = $('#entityType').combobox('getValue');
    var defaultColunms = UploadList.initColumn();
    var table = new BSTable(UploadList.id, Sys.backendPath + "/sds/drgsauto/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams({uploadState: uploadState, entityType: entityType, startDate: startDate, endDate: endDate});
    table.setShowColumns(false);
    table.setShowRefresh(false);
    table.setShowToggle(false);
    table.setPageSize(15);
    table.setSubHeight(-27);
    // table.setOnDblClickRow(UploadList.qcFormShow); //双击数据行打开表单填报

    UploadList.table = table.init();
});


//在Jquery里格式化Date日期时间数据
function timeStamp2String(time) {
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    return year + "-" + month + "-" + date;
}